/**
 * 
 */
package com.training1.core.modules.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.training1.core.common.entity.BOXUserEntity;
import com.training1.core.common.global.signIn.BOXLoginInfoUtil;
import com.training1.core.common.service.CrudService;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.common.utils.excel.ExcelUtil;
import com.training1.core.entity.ContractAttachEntity;
import com.training1.core.entity.ContractBaseEntity;
import com.training1.core.entity.FirContractEntity;
import com.training1.core.entity.FirSecContractExeEntity;
import com.training1.core.entity.PaymentEntity;
import com.training1.core.entity.UnderCompanyEntity;
import com.training1.core.modules.dao.ContractAttachDao;
import com.training1.core.modules.dao.ContractDao;
import com.training1.core.modules.dao.FirContractDao;
import com.training1.core.modules.dao.FirSecContractExeDao;
import com.training1.core.modules.dao.UserDao;


@Service
@Transactional
public class FirContractService extends CrudService<FirContractDao,FirContractEntity>{
	
	@Resource
	private FirContractDao firContractDao;
	@Resource
	private ContractAttachDao contractAttachDao;
	@Resource
	private FirSecContractExeDao firContractExeDao;
	
	
	/**
	 * 根据条件查询一审的合同
	 */
	
	public List<FirContractEntity> findAllFirContract(Map<String,Object> map){
		
		List<FirContractEntity> firContractes = firContractDao.findAllFirContract(map);
		
		
		return firContractes;
	}
	
	/**
	 * 根据合同id 查询合同的详情
	 */
	
	public FirSecContractExeEntity findContractExeById(Map<String,Object> map){
		
		FirSecContractExeEntity FirContractExe = firContractExeDao.findFirContractById(map);
		//查询该合同的所有附件
		List<ContractAttachEntity> contractAttaches = contractAttachDao.findContractAttachEntityById(map);
		FirContractExe.setContractAttaches(contractAttaches);
		
		return FirContractExe;
	}
	
	/**
	 * 导出excel
	 * @param response
	 * @param param
	 */
	public void export(HttpServletResponse response,Map<String, Object> param) {
		List<FirContractEntity> pList = firContractDao.findFirExportList(param);//根据dao中的内容用方法
		//设置列标题
		List<String> excelHead = new ArrayList<String>();
		excelHead.add("合同编号");
		excelHead.add("客户姓名");
		excelHead.add("所属公司");
		excelHead.add("客户手机");
		excelHead.add("拟借金额");
		excelHead.add("申请日期");
		excelHead.add("服务期数");
		
		//设置内容
		List<List<String>> dataList = new ArrayList<List<String>>();
		for(FirContractEntity fc : pList){
			List<String> data = new ArrayList<String>();
			
			data.add(fc.getId().toString());
			data.add(fc.getUserName());
			data.add(fc.getCompanyName());
			data.add(fc.getCellphone());
			data.add(fc.getExceptAmount().toString());
			data.add(fc.getAppDateStr());
			data.add(String.valueOf(fc.getCyc()));
			
			
		
			dataList.add(data);
		}
		
		//设置文件名
		String fileName;
		String sheetName = "sheet1";
		String titleName;
		fileName = "合同一审_"+DateUtils.getDate().replace("-", "");
		titleName = "合同一审_"+DateUtils.getDate().replace("-", "");
		
		ExcelUtil.exportExcel(response, excelHead, dataList, fileName, sheetName,titleName);
	}
	
	/**
	 * 拒绝后 更新基本合同表
	 * @param map
	 * @return
	 */
	public String refuse(Map<String,Object> map){
		
		firContractExeDao.updateContractRefuse(map);
		
		
		return "1";
		
	}
	/**
	 * 同意后 更新基本合同表
	 * @param map
	 * @return
	 */
	public String agree(Map<String,Object> map){
		
		firContractExeDao.updateContractAgree(map);
		
		
		return "1";
		
	}
	
}
